package m2team.com.zuatet;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.daimajia.swipe.SimpleSwipeListener;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;

import java.util.ArrayList;

import me.grantland.widget.AutofitHelper;
import me.grantland.widget.AutofitTextView;

/**
 * Created by Hoang Minh on 12/26/2014.
 **/
public class ListViewAdapter extends BaseSwipeAdapter {
    private Context mContext;
    private ArrayList<String> contentList;
    private int lastPosition = -1;

    public ListViewAdapter(Context mContext,ArrayList<String> contentList) {
        this.mContext = mContext;
        this.contentList = contentList;
    }

    @Override
    public int getCount() {
        return contentList.size();
    }

    @Override
    public String getItem(int position) {
        return contentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getSwipeLayoutResourceId(int i) {
        return R.id.swipe;
    }

    @Override
    public View generateView(int i, ViewGroup viewGroup) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null);
        SwipeLayout swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(i));
        swipeLayout.addSwipeListener(new SimpleSwipeListener() {
            @Override
            public void onOpen(SwipeLayout layout) {
                //YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash));
                Toast.makeText(mContext, "Open", Toast.LENGTH_SHORT).show();
            }
        });
        swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
            @Override
            public void onDoubleClick(SwipeLayout layout, boolean surface) {
                Toast.makeText(mContext, "DoubleClick", Toast.LENGTH_SHORT).show();
            }
        });
        Animation animation = AnimationUtils.loadAnimation(mContext, (i > lastPosition) ? R.anim.up_from_bottom : R.anim.up_from_bottom);
        v.startAnimation(animation);
        lastPosition = i;
        return v;
    }

    @Override
    public void fillValues(final int i, View view) {
        ImageView imgCopy = (ImageView) view.findViewById(R.id.copy);
        imgCopy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager clipboardManager = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                clipboardManager.setPrimaryClip(ClipData.newPlainText("Copy text",getItem(i)));
                Toast.makeText(mContext, "Copy to clipboard", Toast.LENGTH_SHORT).show();
            }
        });
        ImageView imgFavor = (ImageView) view.findViewById(R.id.favor);
        imgFavor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences preferences = mContext.getSharedPreferences("favor", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("favorites", getItem(i));
                editor.commit();
                Toast.makeText(mContext, "Add to favorites", Toast.LENGTH_SHORT).show();
            }
        });
        ImageView imgSend = (ImageView) view.findViewById(R.id.send);
        imgSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.putExtra(Intent.EXTRA_TEXT, getItem(i));
                intent.putExtra(Intent.EXTRA_TITLE,"title: " +  getItem(i));
                intent.setType("text/plain");
                mContext.startActivity(intent);
            }
        });
        AutofitTextView viewById = (AutofitTextView) view.findViewById(R.id.position);
        String item = getItem(i);

        viewById.setText(String.format(item, "Tao", "mày"));
    }

}
